<template>
  <div class="login-box">
    <van-form @submit="onSubmit">
      <van-cell-group inset>
        <van-field
          v-model="formDate.username"
          name="用户名"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="formDate.password"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
      </van-cell-group>
      <div style="margin: 16px">
        <van-button round block type="primary" native-type="submit">
          提交
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script setup lang="ts">
// 声明变量的方式
import { ref } from 'vue'
import { showNotify } from 'vant';
import 'vant/es/notify/style';
import Cookie from 'js-cookie'
import { apiLogin } from '@/api'

// 定义变量
const formDate = ref({username: '', password: ''})

const onSubmit = ()=> {
  apiLogin(formDate.value).then((res:any) => {
    console.log(res)
        // 成功提示
        showNotify({type: 'success', message: res.msg})

        // 存储token
        Cookie.set('token', res.token)

        // 跳转页面
        setTimeout(() => {
            window.location.href = '/'
        }, 1000)
    }).catch(err => {
        showNotify({type: 'danger', message: err.response.data.msg})

    })
}

</script>

<style lang="scss" scoped>
.login-box {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}
</style>
